var pickerFile = require('../../../../../../ucenter/lib/picker_datetime/js/picker_datetime.js');
const util = require('../../../../../../utils/util.js');
const api = require('../../../../../../config/api.js');
const user = require('../../../../../../services/user.js');

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false, //开启付费设置
    experienceChecked: false, //开启体验圈子
    switchadd: '', //开启付费按钮样式
    switchadd2: '', //开启体验圈子按钮样式
    activeName: '',
    activeNames: [''],
    radio: '0',
    tiyanradio: '0', //体验radio
    circleid: '',
    tuikuanopts: [],
    returnMoneyDays: [],// 退款天数
    moneyDays: [],//费用周期天数
    tuikuandays: "1",
    tuikuandays_text: "活动开始1天可退款",
    flag: true,
    experienceDay: "1"
  },

  /**
   * 初始化指定天数
   */
  onLoadDays: function () {
    var _this = this;
    var moneyDays = [];
    var returnMoneyDays = [];
    for (var i = 1; i <= 365; i++) {
      moneyDays.push({ "id": i, "name": i + "天" });
    }
    for (var i = 1; i <= 15; i++) {
      returnMoneyDays.push({ "id": i, "name": i + "天" });
    }
    _this.setData({
      moneyDays: moneyDays,
      returnMoneyDays: returnMoneyDays
    })
  },

  /**
   * 票种确定校验
   */
  miaozhongVerification: function () {
    var _this = this;
    var tuikuanopts = _this.data.tuikuanopts;
    for (var i = 0; i < tuikuanopts.length; i++) {
      if (!tuikuanopts[i].piaozhongsubmit) {
        var piaoZhongName = tuikuanopts[i].name;
        if (piaoZhongName != null && String(piaoZhongName).length > 0) {
          util.dialogAlert(piaoZhongName + '未通过提交确定');
          return false;
        } else {
          util.dialogAlert("有未通过提交确定的票种");
          return false;
        }
      }
    }
    return true;
  },

  /**
   * 保存设置校验
   */
  saveVerification: function() {
    var _this = this;
    var fufeichecked = _this.data.checked;
    var experienceChecked = _this.data.experienceChecked;
    // 开启了付费设置，进行校验票种
    if (fufeichecked && !_this.miaozhongVerification()) {
      return false;
    }

    // 开启了体验圈子，指定天数为null
    if (experienceChecked && '2' == _this.data.tiyanradio && _this.data.experienceDay.length <= 0) {
      util.dialogAlert("请输入指定天数");
      return false;
    }
    return true;
  },

  /**
   * 校验优惠时间
   */
  checkPreferentialdate: function(bindex){
    var _this = this;
    var tuikuanopts = _this.data.tuikuanopts;
    var preferentialdateStart = tuikuanopts[bindex].preferentialdateStart;
    var preferentialdateEnd = tuikuanopts[bindex].preferentialdateEnd;
    if (preferentialdateStart != null && preferentialdateStart.length > 0){
      if (preferentialdateStart == null || preferentialdateStart.length <= 0) {
        util.dialogAlert("请选择" + _this.getPriceText(bindex) + "开始时间");
        return false;
      }
      if (preferentialdateEnd == null || preferentialdateEnd.length <= 0) {
        util.dialogAlert("请选择" + _this.getPriceText(bindex) + "结束时间");
        return false;
      }

      var thisPreferentialdateStart = preferentialdateStart.replace(/年/g, "/").replace(/月/g, "/").replace(/日/g, "");
      var thisPreferentialdateEnd = preferentialdateEnd.replace(/年/g, "/").replace(/月/g, "/").replace(/日/g, "");
      var dateStartTime = new Date(thisPreferentialdateStart).getTime();
      var dateEndTime = new Date(thisPreferentialdateEnd).getTime();
      var dateThisTime = new Date().getTime();
      if (dateStartTime < dateThisTime) {
        util.dialogAlert(_this.getPriceText(bindex) + "开始时间不能小于当前时间");
        return false
      }
      if (dateEndTime <= dateStartTime) {
        util.dialogAlert(_this.getPriceText(bindex) + "结束时间不能小于优惠开始时间");
        return false
      }
    }
    return true;
  },

  /**
   * 计算价格显示
   */
  getShowTitleText: function (bindex){
    var _this = this;
    var tuikuanopts = _this.data.tuikuanopts;
    var price = tuikuanopts[bindex].price;// 原价
    var orangeShell = tuikuanopts[bindex].orangeShell;// 橙贝数量
    var preferentialPrice = tuikuanopts[bindex].preferentialPrice;// 优惠价格
    var preferentialSettings = tuikuanopts[bindex].preferentialSettings;// 优惠设置
    var yuanjia = "免费";
    var realprice = "免费";
    if (preferentialSettings == "0") {
      // 打折优惠
      if (preferentialPrice > 0) {
        yuanjia = "￥" + preferentialPrice;
        realprice = preferentialPrice;
      } else if (price > 0) {
        yuanjia = "￥" + price;
        realprice = price;
      }
    } else if (preferentialSettings == "1") {
      // 团购优惠
      yuanjia = "团购￥" + preferentialPrice;
      realprice = preferentialPrice;
    } else if (preferentialSettings == "2") {
      //橙贝兑换
      yuanjia = "兑换￥" + (preferentialPrice > 0 ? preferentialPrice : price) + "+" + orangeShell + "橙贝";
      realprice = (preferentialPrice > 0 ? preferentialPrice : price) + "+" + orangeShell + "橙贝";
    }
    _this.setData({
      ['tuikuanopts[' + bindex + '].yuanjia']: yuanjia,
      ['tuikuanopts[' + bindex + '].realprice']: realprice
    })
  },

  /**
   * 票种确定按钮点击事件
   */
  submitmiaozhong: function(e) {
    var _this = this;
    // 校验原价
    if (!_this.priceBlur(e)){
      return false;
    }

    // 校验优惠价格
    if (!_this.preferentialPriceBlur(e)){
      return false;
    }
    
    // 名额数量校验
    if (!_this.numberPlacesBlur(e)) {
      return false;
    }

    // 限购设置
    if (!_this.restrictionSettingBlur(e)) {
      return false;
    }

    // 分销佣金校验
    if (!_this.commissionBlur(e)) {
      return false;
    }

    // 体验圈子指定天数校验
    if (!_this.experienceDayBlur(e)){
      return false
    }

    var bindex = e.currentTarget.dataset.bindex;
    var tuikuanopts = _this.data.tuikuanopts;
    var tuikuanopts_name = tuikuanopts[bindex].name;// 票种名字
    var orangeShell = tuikuanopts[bindex].orangeShell;// 橙贝数量
    var preferentialSettings = tuikuanopts[bindex].preferentialSettings;// 优惠设置
    if (!_this.checkPreferentialdate(bindex)){
      // 校验优惠时间
      return false;
    }

    if ('2' == preferentialSettings && !(orangeShell > 0)) {
      // 橙贝兑换
      util.dialogAlert("请输入橙贝数");
      return false;
    }
    
    //当前票种显示属性名称
    _this.getShowTitleText(bindex);

      //关闭折叠版
    var activeNames = _this.data.activeNames;
    _this.remove(activeNames, bindex + 1);
    _this.setData({
      activeNames: activeNames,
      ['tuikuanopts[' + bindex + '].piaozhongsubmit']:true
    })
    // 票种名字为空的时候要默认一个名字
    if (tuikuanopts_name == null || tuikuanopts_name.length <= 0) {
      _this.setData({
        ["tuikuanopts[" + bindex + "].name"]: "票种" + (bindex + 1)
      })
    }
  },

  /**
   * 保存设置
   */
  saveRegistrationfee: function(e) {
    var _this = this;
    if (_this.saveVerification()) {
      var openid = app.globalData.logininfo.openid;
      if (openid != null && openid != "") {
        // 已登录
        util.post(api.fetchToken()).then(function(res) {
          if (res.access_token != '') {
            return Promise.resolve({
              access_token: res.access_token
            });
          }
        }).then(function (res) {
          var param = {};
          param.access_token = res.access_token;
          param.openid = openid;
          param.circleid = _this.data.circleid;
          param.feeSettingSwitch = _this.data.checked; //发费设置开关
          param.experienceSwitch = _this.data.experienceChecked;
          if (param.experienceSwitch) {
            param.experienceCycle = _this.data.tiyanradio;
            if ('2' == param.experienceCycle) {
              param.experienceDay = _this.data.experienceDay
            }
          }
          if (param.feeSettingSwitch) { //如果开启了付费设置的按钮才传值
            param.refundSettings = _this.data.radio;
            if ('0' == param.refundSettings) {
              param.refundData = _this.data.tuikuandays
            }
            param.tuikuanopts = JSON.stringify(_this.data.tuikuanopts);
          }

          util.post(api.Interest_insertAmount, param).then(function(res2) {
            if (res2.code == 200) {
              var tuikuanopts = _this.data.tuikuanopts;
              var pages = getCurrentPages();
              var prevPage = pages[pages.length - 2];
              if (tuikuanopts.length > 0) {
                //只有一个票种
                if (tuikuanopts.length==1){
                  if (''!=tuikuanopts[0].price){
                    if (''!=tuikuanopts[0].preferentialPrice){
                      prevPage.setData({
                        money: '￥' + tuikuanopts[0].preferentialPrice + '-' + tuikuanopts[0].price
                      })
                    }else{
                      prevPage.setData({
                        money: '￥' + tuikuanopts[0].price
                      })
                    }
                  }
                }else{
                  //最大值
                  var max = 0;
                  var min = 0;
                  for (var i = 0; i < tuikuanopts.length; i++) {
                    if ('' != tuikuanopts[i].price) {
                      if ('' != tuikuanopts[i].preferentialPrice){
                        max = tuikuanopts[i].preferentialPrice
                      }else{
                        max = tuikuanopts[i].price
                      }
                      break;
                    }
                  }

                  if(max!=0){
                    //判断是否有免费票种
                    var flag=false;
                    for (var i = 0; i < tuikuanopts.length; i++) {
                      if ('' == tuikuanopts[i].price || '0' == tuikuanopts[i].price){
                        flag=true;
                      }
                      if ('' == tuikuanopts[i].preferentialPrice || '0' == tuikuanopts[i].preferentialPrice){
                        flag=true;
                      }
                    }
                    if (!flag){
                      for (var i = 0; i < tuikuanopts.length; i++) {
                        if ('' != tuikuanopts[i].preferentialPrice) {
                          min = tuikuanopts[i].preferentialPrice
                          break;
                        } else {
                          if ('' != tuikuanopts[i].price) {
                            min = tuikuanopts[i].price
                            break;
                          }
                        }
                      }
                    }
                    

                    for (var i = 0; i < tuikuanopts.length; i++) {
                      if ('' != tuikuanopts[i].price){
                        //找出最大值
                        if ('' != tuikuanopts[i].preferentialPrice){
                          if (parseInt(tuikuanopts[i].preferentialPrice) > parseInt(max)){
                            max = tuikuanopts[i].preferentialPrice;
                          }
                          if (parseInt(tuikuanopts[i].preferentialPrice) < parseInt(min)) {
                            min = tuikuanopts[i].preferentialPrice;
                          }
                        }else{
                          if (parseInt(tuikuanopts[i].price) > parseInt(max)) {
                            max = tuikuanopts[i].price;
                          }
                          if (parseInt(tuikuanopts[i].price) > parseInt(min)) {
                            min = tuikuanopts[i].price;
                          }
                        }
                      }
                    }

                    if (flag){
                      prevPage.setData({
                        money: '￥' + 0 + '-' + max
                      })
                    }else{
                      if (parseInt(min) == parseInt(max)){
                        prevPage.setData({
                          money: '￥' + max
                        })
                      }else{
                        prevPage.setData({
                          money: '￥' + min + '-' + max
                        })
                      }
                    }
                  }
                }
              }
              wx.showToast({
                title: '保存成功',
                icon: 'none',
                duration: 2000
              })
              wx.navigateBack({
                //返回上一级
                delta: 1,
              })
            } else {
              wx.showToast({
                title: '数据异常',
                icon: 'none',
                duration: 2000
              })
            }
          })
        })
      }
    }
  },

  /**
   * 分销佣金输入事件
   */
  commissionInput: function(e) {
    var _this = this;
    var value = String(e.detail.value).trim();
    var bindex = e.currentTarget.dataset.bindex;
    _this.setData({
      ['tuikuanopts[' + bindex + '].commission']: parseInt(value),
      ['tuikuanopts[' + bindex + '].piaozhongsubmit']: false
    })
  },

  /**
   * 名额数量输入事件
   */
  numberPlacesInput: function (e) {
    var _this = this;
    var value = String(e.detail.value).trim();
    var bindex = e.currentTarget.dataset.bindex;
    _this.setData({
      ['tuikuanopts[' + bindex + '].numberPlaces']: parseInt(value),
      ['tuikuanopts[' + bindex + '].piaozhongsubmit']: false
    })
  },

  /**
   * 限购数量输入事件
   */
  restrictionSettingInput: function (e) {
    var _this = this;
    var value = String(e.detail.value).trim();
    var bindex = e.currentTarget.dataset.bindex;
    _this.setData({
      ['tuikuanopts[' + bindex + '].restrictionSetting']: parseInt(value),
      ['tuikuanopts[' + bindex + '].piaozhongsubmit']: false
    })
  },

  /**
   * 橙贝数输入事件
   */
  orangeShellInput: function(e) {
    var _this = this;
    var value = String(e.detail.value).trim();
    var bindex = e.currentTarget.dataset.bindex;
    _this.setData({
      ['tuikuanopts[' + bindex + '].orangeShell']: parseInt(value),
      ['tuikuanopts[' + bindex + '].piaozhongsubmit']: false
    })
  },

  /**
   * 优惠价格输入事件
   */
  preferentialPriceInput: function(e) {
    var _this = this;
    var value = String(e.detail.value).trim();
    var bindex = e.currentTarget.dataset.bindex;
    _this.setData({
      ['tuikuanopts[' + bindex + '].preferentialPrice']: value,
      ['tuikuanopts[' + bindex + '].piaozhongsubmit']: false
    })
  },

  /**
   * 票种名称输入事件
   */
  ticketNameInput: function(e) {
    var _this = this;
    var value = e.detail.value;
    var bindex = e.currentTarget.dataset.bindex;
    _this.setData({
      ['tuikuanopts[' + bindex + '].name']: value,
      ['tuikuanopts[' + bindex + '].piaozhongsubmit']: false
    })
  },

  /**
   * 原价金额输入事件
   */
  priceInput: function (e) {
    var _this = this;
    var value = String(e.detail.value).trim();
    var bindex = e.currentTarget.dataset.bindex;
    _this.setData({
      ['tuikuanopts[' + bindex + '].price']: value,
      ['tuikuanopts[' + bindex + '].piaozhongsubmit']: false
    })
  },

  /**
   * 指定天数输入事件
   */
  experienceCycleDayInput: function(e) {
    var _this = this;
    var value = String(e.detail.value).trim();
    _this.setData({
      experienceDay: parseInt(value)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //加载时间
    var _this = this;
    this.datetimePicker = new pickerFile.pickerDatetime({
      page: this,
      animation: 'slide',
      duration: 500
    });
    this.setData({
      circleid: options.circleid
    })
    _this.getAmountInfo();
    _this.onLoadDays();
  },
  
  /**
   * 加载页面数据
   */
  getAmountInfo: function() {
    var _this = this;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function(res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function(res) {
        let param = {};
        param.access_token = res.access_token;
        param.openid = openid;
        param.circleid = _this.data.circleid;
        util.post(api.Interest_getAmountInfo, param).then(function(res2) {
          if (res2.code == 200) {
            if (res2.result.checked == 1) {
              _this.setData({
                checked: true,
                switchadd: 'switchview2'
              })
            }
            if ( res2.result.experienceChecked == 1) {
              _this.setData({
                experienceChecked: true,
                switchadd2: 'switchview2'
              })
            }
            if (null != res2.result.radio) {
              _this.setData({
                radio: String(res2.result.radio)
              })
              if (res2.result.radio == 0) {
                if (null != res2.result.tuikuandays) {
                  _this.setData({
                    tuikuandays: String(res2.result.tuikuandays)
                  })
                  _this.onChangeRadio_js();
                }
              }
              if (null != res2.result.tiyanradio) {
                _this.setData({
                  tiyanradio: String(res2.result.tiyanradio)
                })
              }
              if (res2.result.tiyanradio == 2) {
                if (null != res2.result.experienceDay) {
                  _this.setData({
                    experienceDay: res2.result.experienceDay
                  })
                }
              }
              if (res2.result.tuikuanopts != null) {
                _this.setData({
                  tuikuanopts: res2.result.tuikuanopts
                })
              }
            }
            // 如果没有表单设置信息，需要默认添加一个
            var tuikuanopts = _this.data.tuikuanopts;
            var checked = _this.data.checked;
            if (tuikuanopts.length <= 0 && checked == true){
              _this.addopt();
            }
          } else {
            wx.showToast({
              title: '数据异常',
              icon: 'none',
              duration: 2000
            })
          }
        })
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    var _this = this;
    var fufeichecked = _this.data.checked;
    if (!fufeichecked){
      // 返回上一页，如果没有选中收费，就进行保存
      _this.saveRegistrationfee();
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 付费设置点击事件
   */
  onChange: function(e){
    var _this = this;
    var detail = e.detail;
    _this.setData({
      checked: detail,
      switchadd: detail ? 'switchview2' : ''
    })
    var checked = _this.data.checked;
    var tuikuanopts = _this.data.tuikuanopts;
    if (tuikuanopts.length <= 0 && checked == true) {
      _this.addopt();
    }
  },

  /**
   * 开启体验圈子点击事件
   */
  onExperienceChange: function(e){
    var _this = this;
    var detail = e.detail;
    var tiyanradio = _this.data.tiyanradio;
    _this.setData({
      experienceChecked: detail,
      switchadd2: detail ? 'switchview2' : '',
      tiyanradio: tiyanradio == null || tiyanradio.length <= 0 ? "0" : tiyanradio
    })
  },
  
  /**
   * 退款设置组件点击事件
   */
  onChangeActiveName(event) {
    this.setData({
      activeName: event.detail
    });
  },

  /**
   * 退款设置选中事件
   */
  onChangeRadio: function(event) {
    var _this = this;
    _this.setData({
      radio: event.detail
    })
    _this.onChangeRadio_js();
  },

  /**
   * 计算退款设置的文字回显
   */
  onChangeRadio_js: function () {
    var _this = this;
    var radio = _this.data.radio;
    var tuikuandays = _this.data.tuikuandays;
    if (radio == 0) {
      // 活动开始前可退款
      _this.setData({
        tuikuandays_text: "活动开始前" + tuikuandays + "天可退款"
      })
    } else {
      // 不支持退款
      _this.setData({
        tuikuandays_text: "不支持退款"
      })
    }
  },

  onChangetiyanRadio: function(event) {
    this.setData({
      tiyanradio: event.detail
    })
  },
  onChangeActiveNames: function(event) {
    var active = this.data.activeNames
    this.setData({
      activeNames: event.detail
    })
  },
  onChangeRadio3: function(event) {
    var bindex = parseInt(event.currentTarget.dataset.bindex);
    var thisOpt = 'tuikuanopts[' + bindex + '].preferentialSettings';
    var piaozhongsubmit = 'tuikuanopts[' + bindex + '].piaozhongsubmit';
    this.setData({
      [thisOpt]: event.detail,
      [piaozhongsubmit]:false
    })
  },
  onChangeRadio2: function(event) {
    var bindex = parseInt(event.currentTarget.dataset.bindex);
    var thisOpt = 'tuikuanopts[' + bindex + '].costCycle';
    var piaozhongsubmit = 'tuikuanopts[' + bindex + '].piaozhongsubmit';
    this.setData({
      [thisOpt]: event.detail,
      [piaozhongsubmit]: false
    })
  },

  /**
   * 添加票种事件
   */
  addopt: function() {
    var that = this;
    var pickerPreDate = new Date();
    let str = {
      id: '', //回显时有id用于删除
      preferentialSettings: '0', //优惠设置
      costCycle: '0', //费用周期
      designatedDays: "", //费用周期自定义天数
      designatedDaysName: "1天", //用户展示自定义天数
      preferentialdateStart: "", //优惠时间
      preferentialdateEnd: "", //结束时间
      name: '', //票种名称
      price: '', //原价金额
      preferentialPrice: '', //优惠价格
      orangeShell: '', //兑换橙贝数
      restrictionSetting: '1', //限购设置
      commission: '', //分销佣金
      yuanjia: '免费',
      piaozhongsubmit: false,//票种是否已经提交
      realprice:'免费',//存数据库的真实需要付款金额
    };
    let arr = this.data.tuikuanopts;
    if(arr.length>=12){
      wx.showToast({
        title: '一个圈子只能有12种票种',
        icon: 'none',
        duration: 2000
      })
    }else{
      arr.push(str);
      this.setData({
        tuikuanopts: arr
      })
    }
  },
  delOpt: function(event) {
    var bindex = parseInt(event.currentTarget.dataset.bindex);
    var currPage = this;
    wx.showModal({
      title: "提示",
      content: "是否删除该票种",
      success: function(res) {
        if (res.confirm) {
          var thisopts = currPage.data.tuikuanopts;
          if(thisopts.length==1){
            wx.showToast({
              title: '只剩一个票种不可删除',
              icon: 'none',
              duration: 2000
            })
          }else{
            thisopts.splice(bindex, 1);
            var activeNames = currPage.data.activeNames;
            currPage.remove(activeNames, bindex + 1);
            currPage.setData({
              tuikuanopts: thisopts,
              activeNames: activeNames
            })
          }
        }
      }
    })
  },

  /**
   * 退款天数选中事件
   */
  daysChange: function(e) {
    var _this = this;
    var index = e.detail.value;
    var daysinfo = _this.data.returnMoneyDays[index];
    var days_id = daysinfo.id;
    var days_name = daysinfo.name;
    _this.setData({
      tuikuandays: days_id
    })
    _this.onChangeRadio_js(); 
  },

  /**
   * 费用周期指定天数选中事件
   */
  zhidingdaysChange: function(e) {
    var _this = this;
    var index = e.detail.value;
    var daysinfo = _this.data.moneyDays[index];
    var days_id = daysinfo.id;
    var days_name = daysinfo.name;
    var bindex = parseInt(e.currentTarget.dataset.bindex);
    var thisOpt = 'tuikuanopts[' + bindex + '].designatedDays';
    var thisOptName = 'tuikuanopts[' + bindex + '].designatedDaysName';
    var piaozhongsubmit = 'tuikuanopts[' + bindex + '].piaozhongsubmit';
    _this.setData({
      [thisOpt]: days_id,
      [thisOptName]: days_name,
      [piaozhongsubmit]:false
    })
  },

  /**
   * 票种的优惠开始时间点击事件
   */
  startDateTap: function (e) {
    var _this = this;
    var bindex = e.currentTarget.dataset.bindex;
    var preferentialdateStart = _this.data.tuikuanopts[bindex].preferentialdateStart;
    if (preferentialdateStart.length <= 0) {
      preferentialdateStart = util.haveSomeMinutesTime(60);
    }
    _this.datetimePicker.setPicker('tuikuanopts[' + bindex + '].preferentialdateStart', preferentialdateStart);
    _this.setData({
      flag: false,
      ['tuikuanopts[' + bindex + '].piaozhongsubmit']: false,
    })
  },

/**
   * 票种的优惠结束时间点击事件
   */
  endDateTap: function(e) {
    var _this = this;
    var bindex = parseInt(e.currentTarget.dataset.bindex);
    var piaozhongsubmit = 'tuikuanopts[' + bindex + '].piaozhongsubmit';
    var preferentialdateEnd = _this.data.tuikuanopts[bindex].preferentialdateEnd;
    if (preferentialdateEnd.length <= 0) {
      preferentialdateEnd = util.haveSomeMinutesTime(120);
    }
    _this.datetimePicker.setPicker('tuikuanopts[' + bindex + '].preferentialdateEnd', preferentialdateEnd);
    _this.setData({
      flag: false,
      [piaozhongsubmit]: false
    })
  },

  remove: function(array, val) {
    for (var i = 0; i < array.length; i++) {
      if (array[i] == val) {
        array.splice(i, 1);

      }
    }
    return -1;
  },

  stopPageScroll() {
    return
  },

  /**
   * 获取价格的文字
   */
  getPriceText: function (bindex) {
    var _this = this;
    var preferentialSettings = _this.data.tuikuanopts[bindex].preferentialSettings;
    if ("0" == preferentialSettings) {
      return "优惠";
    } else if ("1" == preferentialSettings) {
      return "团购";
    } else {
      return "兑换";
    }
  },

  /**
   * 校验优惠价格
   */
  preferentialPriceBlur: function (e) {
    var _this = this;
    var bindex = e.currentTarget.dataset.bindex;
    var preferentialPrice = _this.data.tuikuanopts[bindex].preferentialPrice;
    if (preferentialPrice != null && String(preferentialPrice).length > 0) {
      preferentialPrice = Number(preferentialPrice);
      if (!(preferentialPrice >= 0)) {
        util.dialogAlert("请输入有效的" + _this.getPriceText(bindex) + "价格");
        return false;
      }

      var price = _this.data.tuikuanopts[bindex].price;
      if (price != null && String(price).length > 0) {
        price = Number(price);
        if (price <= preferentialPrice) {
          util.dialogAlert(_this.getPriceText(bindex) + "价格必须比原价金额小");
          return false;
        }
      } else {
        util.dialogAlert("请先输入原价金额");
        return false;
      }
    }else{
      var preferentialSettings = _this.data.tuikuanopts[bindex].preferentialSettings;
      if (preferentialSettings == "1") {
        // 不是打折优惠
        util.dialogAlert("请输入" + _this.getPriceText(bindex) + "价格");
        return false;
      }
    }
    return true;
  },

  /**
   * 校验原价
   */
  priceBlur: function (e) {
    var _this = this;
    var bindex = e.currentTarget.dataset.bindex;
    var price = _this.data.tuikuanopts[bindex].price;
    var preferentialSettings = _this.data.tuikuanopts[bindex].preferentialSettings;
    if (price != null && String(price).length > 0) {
      if (!(price >= 0)) {
        util.dialogAlert("请输入有效的原价金额");
        return false;
      }
      if (preferentialSettings != "0" && price == 0){
        util.dialogAlert("请输入大于0的原价金额");
        return false;
      }
      if (price >= 10000) {
        util.dialogAlert("原价金额不可大于万元");
        return false;
      }
    }else{
      if (preferentialSettings != "0"){
        // 不是打折优惠
        util.dialogAlert("请输入原价金额");
        return false;
      }
    }
    return true;
  },

  /**
   * 名额数量校验
   */
  numberPlacesBlur: function (e) {
    var _this = this;
    var bindex = e.currentTarget.dataset.bindex;
    var numberPlaces = _this.data.tuikuanopts[bindex].numberPlaces;
    if (numberPlaces != null && String(numberPlaces).length > 0) {
      if (!(numberPlaces > 0)) {
        util.dialogAlert("请输入有效的名额数量");
        return false;
      }
    }
    return true;
  },

  /**
   * 限购设置
   */
  restrictionSettingBlur: function (e) {
    var _this = this;
    var bindex = e.currentTarget.dataset.bindex;
    var restrictionSetting = _this.data.tuikuanopts[bindex].restrictionSetting;
    if (restrictionSetting != null && String(restrictionSetting).length > 0) {
      if (!(restrictionSetting > 0)) {
        util.dialogAlert("请输入有效的限购数量");
        return false;
      }
    }else{
      util.dialogAlert("请输入限购数量");
      return false;
    }
    return true;
  },

  /**
   * 分销佣金校验
   */
  commissionBlur: function (e) {
    var _this = this;
    var bindex = parseInt(e.currentTarget.dataset.bindex);
    var commission = _this.data.tuikuanopts[bindex].commission;
    if (commission != null && String(commission).length > 0 && commission > 0) {
      commission = Number(commission);
      if (!(commission >= 0)) {
        util.dialogAlert("请输入有效的分销佣金");
        return false;
      }

      var preferentialPrice = _this.data.tuikuanopts[bindex].preferentialPrice;
      if (preferentialPrice != null && String(preferentialPrice).length > 0) {
        preferentialPrice = Number(preferentialPrice);
        if (commission > preferentialPrice) {
          util.dialogAlert(_this.getPriceText(bindex) + "价格必须比分销佣金大");
          return false;
        }
      } else {
        util.dialogAlert("请先输入" + _this.getPriceText(bindex) + "价格");
        return false;
      }
    }
    return true;
  },

  /**
   * 体验圈子指定天数校验
   */
  experienceDayBlur: function (e) {
    var _this = this;
    var experienceDay = _this.data.experienceDay;
    if (experienceDay != null && String(experienceDay).length > 0) {
      if (!(experienceDay >= 0) || experienceDay > parseInt(experienceDay)) {
        util.dialogAlert("请输入有效的体验周期");
        return false;
      }
      if (experienceDay > 365) {
        util.dialogAlert("体验周期不可大于365天");
        return false;
      }
    }
    return true;
  }
})